<template>
    <div v-if="layoutMode">
      <div class="layout" :class="{'layout-hide-text': spanLeft < 5}">
        <Row type="flex">
          <i-col :span="spanLeft" class="layout-menu-left">
            <Menu active-key="1" theme="dark" width="auto" :style="bodyLeftMenuStyle" @on-select="selectMenu">
              <div class="layout-logo-left">
                人才库系统
              </div>
              <Submenu class="menu_item" :key="item.name" v-for="item in viewRoutes" v-if="item.menu&&item.menu.level==2" :name="item.name">
                <template slot="title">
                  <Icon :type="item.menu.icon" :size="iconSize"></Icon>
                  {{item.label}}
                </template>
                <MenuItem :key="i.name" v-for="i in getSubMenu(item.menu.child)" :name="i.name">
                  {{i.label}}
                </MenuItem>
              </Submenu>
              <MenuItem class="menu_item" :key="item.name" v-for="item in viewRoutes" v-if="item.menu&&item.menu.level==1" :name="item.name">
                <Icon :type="item.menu.icon" :size="iconSize"></Icon>
                {{item.label}}
              </MenuItem>
            </Menu>
          </i-col>
          <i-col :span="spanRight">
            <div class="layout-header">
              <i-button type="text" @click="toggleClick">
                <Icon type="navicon" size="32"></Icon>
              </i-button>
              <div class="layout-header-right">
                <Button style="font-size: 15px" type="text" v-text="staffName"></Button>
                <Button style="font-size: 15px" type="text" @click="exit">退出</Button>
              </div>
            </div>
            <div class="layout-breadcrumb">
              <Breadcrumb>
                <BreadcrumbItem>首页</BreadcrumbItem>
                <Breadcrumb-item :key="item.name" v-for="item in getBreadcrumb($route.path)" :href="item.path">
                  {{item.label}}
                </Breadcrumb-item>
              </Breadcrumb>
            </div>
            <div class="layout-content">
              <div :style="bodyContentStyle" class="layout-content-main">
                <router-view/>
              </div>
            </div>
            <div class="layout-copy">
              2017-2020 &copy; 杭州数梦工场科技有限公司
            </div>
          </i-col>
        </Row>
      </div>
    </div>
    <div v-else>
      <router-view/>
    </div>

</template>

<script>

//  import { mapGetters } from 'vuex'
  import routes from './router/routes'

  export default {
    data () {
      return {
        spanLeft: 5,
        spanRight: 19,
        layoutMode: true,
        bodyLeftMenuStyle: 'height:' + (window.innerHeight - 17) + 'px',
        bodyContentStyle: 'min-height:' + (window.innerHeight - 171) + 'px',
        viewRoutes: routes.routes
      }
    },
    computed: {
//      ...mapGetters({loginSessionState: 'auth/getLoginSessionState'}),
      iconSize () {
        return this.spanLeft === 5 ? 14 : 24;
      },
      loginSessionCache () {
        return this.$store.getters['auth/getLoginSessionCache'];
      },
      loginSessionState (){
        return this.$store.getters['auth/getLoginSessionState'];
      },
      staffName (){
        return this.$store.getters['auth/getStaffName'];
      }
    },
    methods: {
      toggleClick () {
        if (this.spanLeft === 5) {
          this.spanLeft = 2;
          this.spanRight = 22;
        } else {
          this.spanLeft = 5;
          this.spanRight = 19;
        }
      },
      getSubMenu (arr) {
        let result = [];
        const grep = this.layoutMode ? 'member' : 'visitor';
        for (let i of arr) {
          for (let f of this.viewRoutes) {
            if (i === f.name && grep === f.grep){
              result.push(f);
            }
          }
        }
        return result;
      },
      getBreadcrumb (path) {
        let result = [];
        let node = null;
        for (let i = 0; i < this.viewRoutes.length; i++) {
          if (path === this.viewRoutes[i].path) {
            node = this.viewRoutes[i];
            result.push(node);
            while (node.menu && node.menu.parent) {
              for (let j = 0; j < this.viewRoutes.length; j++) {
                if (node.menu.parent === this.viewRoutes[j].name) {
                  node = this.viewRoutes[j];
                  result.splice(0, 0, node);
                  break;
                }
              }
            }
          }
        }
        return result;
      },
      selectMenu (name) {

        for (let value of this.viewRoutes) {

          if (name === value.name){
            this.$router.push(value.path);
            break;
          }
        }
      },
      initViewState (){
        this.layoutMode = this.loginSessionCache.online;
        if (this.layoutMode){
          this.$router.push('/resume_pool/social_resume');
        }else {
          this.$router.push('/auth');
        }
      },
      exit () {
        this.$store.commit('auth/removeSessionMutation');
        this.$router.push('/auth');
        this.$Message.success('退出成功');
//        window.location.reload();
      }
    },
    watch: {
      loginSessionState: function (newLoginSession) {
        this.layoutMode = newLoginSession.online;
        if (newLoginSession.online){
          this.$Message.success('登录成功');
          this.$router.push('/resume_pool/social_resume');
        }
      }
    },
    mounted(){

    },
    created () {
      this.initViewState();
    }
  }
</script>

<style>
  .layout{
    border: 1px solid #d7dde4;
    background: #f5f7f9;
    position: relative;
    border-radius: 4px;
    overflow: hidden;
  }
  .layout-breadcrumb{
    padding: 10px 15px 0;
  }
  .layout-content{
    min-height: 200px;
    margin: 15px;
    overflow: hidden;
    background: #fff;
    border-radius: 4px;
  }
  .layout-content-main{
    padding: 10px;
  }
  .layout-copy{
    text-align: center;
    padding: 10px 0 20px;
    color: #9ea7b4;
  }
  .layout-menu-left{
    background: #47515f;
  }
  .layout-header{
    height: 60px;
    background: #fff;
    box-shadow: 0 1px 1px rgba(0,0,0,.1);
  }
  .layout-logo-left{
    width: 90%;
    height: 30px;
    background: #5b6270;
    border-radius: 3px;
    margin: 15px auto;
    color: #fff;
    text-align: center;
    line-height: 30px;
  }
  .layout-ceiling-main a{
    color: #9ba7b5;
  }

  .layout-header-right {
    float: right;
    line-height: 60px;
    margin-right: 10px;
  }

</style>
